﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookingDB.Models;
using GenericModels;

namespace CTRLayer
{
    public class CTRPerson
    {
        private BioBookingContext db;

        //constructor
        public CTRPerson()
        {
            db = new BioBookingContext();
        }

        /// <summary>
        /// Retrieves a customer from the database, using the persons phonenumber
        /// </summary>
        /// <param name="phoneNo"></param>
        /// <returns>A customer</returns>
        public Customer GetCustomerByPhoneNo(string phoneNo)
        {
            Customer cus = null;
            try
            {
                EntityCustomer eCust = (from c in db.Customers
                                        where c.Person.phoneNo == phoneNo
                                        select c).SingleOrDefault();

                cus = TranslateCustomer(eCust);
            }
            catch (Exception ex)
            {
                Console.WriteLine("customer not found exception", ex.StackTrace);
            }

            return cus;
        }

        /// <summary>
        /// Retrieves an employee from the database, using the persons employee id
        /// </summary>
        /// <param name="empId"></param>
        /// <returns>An Employee</returns>
        public Employee GetEmployeeById(int empId)
        {
            Employee empl = null;
            try
            {
                EntityEmployee eEmp = (from e in db.Employees
                                       where e.emp_Id == empId
                                       select e).Single();

                empl = TranslateEmployee(eEmp);
            }
            catch (Exception ex)
            {
                Console.WriteLine("employee not found exception", ex.StackTrace);
            }

            return empl;
        }

        /// <summary>
        /// Adds a booking to a customer by inserting the customer id and the booking id in the db table Customer
        /// </summary>
        /// <param name="cust"></param>
        /// <param name="booking"></param>
        /// <returns>a boolean value which indicates whether the transaction was succesful</returns>
        public bool AddBookingToCustomer(Customer cust, Booking booking)
        {
            bool succes = false;
            try
            {
                //EntityCustomer eCust = new EntityCustomer() { cust_Id = cust.CustomerId, booking_Id = booking.BookingId };
                //db.Customers.Add(eCust);
                //db.SaveChanges();
                //succes = true;

                EntityBooking eBook = new EntityBooking() { 
                                                            id = booking.BookingId,
                                                            totalPrice = (decimal)booking.TotalPrice,
                                                            employee_Id = booking.EmpId,
                                                            isCollected = booking.IsCollected,
                                                            customer_id = cust.CustomerId
                                                           };
                db.Bookings.Add(eBook);
                db.SaveChanges();
                succes = true;

            }
            catch(Exception e)
            {
                Console.WriteLine("Error: " + e);
            }

            return succes;


        }


        /// <summary>
        /// Helper method which translates the retrieved entity framework object into a generic model
        /// </summary>
        /// <param name="eCust"></param>
        /// <returns>A Customer</returns>
        private Customer TranslateCustomer(EntityCustomer eCust)
        {
            Customer cust = new Customer();

            cust.Address = eCust.Person.address;
            cust.FName = eCust.Person.firstName;
            cust.LName = eCust.Person.lastName;
            cust.Email = eCust.Person.email;
            cust.PhoneNo = eCust.Person.phoneNo;
            cust.PostalCode =(Int32)eCust.Person.postalCode_Id;
            cust.CustomerId = eCust.cust_Id;

            return cust;
        }

        /// <summary>
        /// Helper method which translates the retrieved entity framework object into a generic model
        /// </summary>
        /// <param name="eEmp"></param>
        /// <returns>An Employee</returns>
        private Employee TranslateEmployee(EntityEmployee eEmp)
        {
            Employee emp = new Employee();


            emp.Address = eEmp.Person.address;
            emp.FName = eEmp.Person.firstName;
            emp.LName = eEmp.Person.lastName;
            emp.Email = eEmp.Person.email;
            emp.PhoneNo = eEmp.Person.phoneNo;
            emp.PostalCode = (Int32)eEmp.Person.postalCode_Id;
            emp.EmployeeId = eEmp.emp_Id;
            emp.Title = eEmp.title;

            return emp;
        }
    }
}
